package zhoufu.sz.sanxiao_check;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ImageLoader;
import cn.finalteam.galleryfinal.ThemeConfig;
import com.baidu.mapapi.SDKInitializer;
import com.cy.src.myftp.uploadcore.TickReceiver;
import com.cy.src.myftp.uploadcore.UploadService;
import com.tencent.bugly.crashreport.CrashReport;
import java.util.LinkedList;
import java.util.List;
import zhoufu.sz.sanxiao_check.config.Constant;
import zhoufu.sz.sanxiao_check.inject.components.AppComponent;
import zhoufu.sz.sanxiao_check.inject.components.DaggerAppComponent;
import zhoufu.sz.sanxiao_check.inject.modules.AppModule;
import zhoufu.sz.sanxiao_check.weiget.GlideImageLoader;

/**
 * Created by hyc on 2016/6/18.
 */
public class App extends Application
{
    private AppComponent _appComponent;

    private static App app;

    @Override
    public void onCreate()
    {
        super.onCreate();
        app = this;
        _appComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();
        AppComponent.Instance.init(_appComponent);
        //http://192.168.1.252:9500/WebService/WangGeTwo.asmx
        //http://120.31.131.105:8091/WebService/WangGeTwo.asmx
        Constant.init("http://120.31.131.105:8002/WebService/WangGeTwo.asmx/", true, "wghxtong");
        configFtp();
        initMap();
        configGalleryFinal();
        CrashReport.initCrashReport(getApplicationContext(), "24d3945282", false);
    }

    private void initMap()
    {
        SDKInitializer.initialize(this);
    }

    //运用list来保存们每一个activity是关键
    private List<Activity> mList = new LinkedList<Activity>();
    //为了实现每次使用该类时不创建新的对象而创建的静态对象

    // add Activity
    public void addActivity(Activity activity)
    {
        mList.add(activity);
    }

    public void removeActovty(Activity activity)
    {
        mList.remove(activity);
    }

    //关闭每一个list内的activity
    public void exit()
    {
        try
        {
            for (Activity activity : mList)
            {
                if ( activity != null )
                {
                    activity.finish();
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            System.exit(0);
        }
    }


    /**
     * 配置图片选择器
     */
    private void configGalleryFinal()
    {
        ThemeConfig themeConfig = new ThemeConfig.Builder()
                .build();
        FunctionConfig functionConfig = new FunctionConfig.Builder()
                .setEnableCamera(true)
                .setEnableEdit(true)
                .setEnableCrop(true)
                .setEnablePreview(true)
                .setCropSquare(true)
                .setForceCrop(true)
                .build();
        ImageLoader imageLoader = new GlideImageLoader();
        CoreConfig config = new CoreConfig.Builder(this, imageLoader, themeConfig)
                .setFunctionConfig(functionConfig)
                .build();
        GalleryFinal.init(config);
    }

    private void configFtp()
    {
        IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(new TickReceiver(), filter);
        UploadService.Config.setUploadHost("120.31.131.190");
        UploadService.Config.setUploadPort(2121);
        UploadService.Config.setDownloadHost("120.31.131.190");
        UploadService.Config.setDownloadPort(8080);
        UploadService.Config.setRemoteDirName("DLSanXiao_V2");
        UploadService.Config.setUsername("admin");
        UploadService.Config.setPassword("21232F297A57A5A743894A0E4A801FC3");
    }

    //杀进程
    public void onLowMemory()
    {
        super.onLowMemory();
        System.gc();
    }

    public AppComponent getAppComponent()
    {
        return _appComponent;
    }

    public static App getApp()
    {
        return app;
    }
}
